package com.lb.jwitter.service;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.jws.WebService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.lb.jwitter.*;
import com.lb.jwitter.hibernate.*;
import com.lb.jwitter.util.ChumanoLogger;
import com.lb.jwitter.util.ConditionReport;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

@Service("tbl_CTDT_ThayTheMonHocService")
@Transactional
@WebService(endpointInterface = "com.lb.jwitter.service.Tbl_CTDT_ThayTheMonHocService")
public class Tbl_CTDT_ThayTheMonHocServiceImpl implements
		Tbl_CTDT_ThayTheMonHocService, Serializable {

	private Log log = LogFactory
			.getLog(Tbl_CTDT_ThayTheMonHocServiceImpl.class);
	private static final long serialVersionUID = -8740095022264495229L;

	@Autowired
	@Qualifier("tbl_CTDT_ThayTheMonHocDAO")
	private Tbl_CTDT_ThayTheMonHocDAO _tbl_CTDT_ThayTheMonHocDao;

	@Autowired
	@Qualifier("tbl_CTDT_MonHocDAO")
	private Tbl_CTDT_MonHocDAO _tbl_CTDT_MonHocDao;

	public final List<Tbl_CTDT_ThayTheMonHoc> findAlltbl_CTDT_ThayTheMonHoc(
			int pageSize, int page) {
		List<Tbl_CTDT_ThayTheMonHoc> result = get_tbl_CTDT_ThayTheMonHocDao()
				.findAll(pageSize, page);
		log.info("Number of user in DB:: " + result.size());
		return result;
	}

	@Transactional(noRollbackFor = IllegalArgumentException.class)
	public final Long createtbl_CTDT_ThayTheMonHoc(Tbl_CTDT_ThayTheMonHoc obj) {
		return _tbl_CTDT_ThayTheMonHocDao.save(obj);
	}

	public final void updatetbl_CTDT_ThayTheMonHoc(Tbl_CTDT_ThayTheMonHoc obj) {
		_tbl_CTDT_ThayTheMonHocDao.update(obj);
	}// end update methode

	public final Tbl_CTDT_ThayTheMonHoc gettbl_CTDT_ThayTheMonHoc(Long id) {
		log.info("Try to retrieve the tbl_CTDT_ThayTheMonHoc: " + id);
		return get_tbl_CTDT_ThayTheMonHocDao().get(id);
	}

	public void deletetbl_CTDT_ThayTheMonHoc(Long id) {
		log.info("Try to delete the tbl_CTDT_ThayTheMonHoc by Id : " + id);
		if (_tbl_CTDT_ThayTheMonHocDao.exists(id)) {
			_tbl_CTDT_ThayTheMonHocDao.delete(id);
		} else {
			throw new IllegalArgumentException("EmployeeId '" + id
					+ "' does not exists");
		}
	}

	public void deleteListtbl_CTDT_ThayTheMonHoc(String[] listId) {
		for (String id : listId) {
			deletetbl_CTDT_ThayTheMonHoc(Long.parseLong(id));
		}
	}

	public List<Tbl_CTDT_ThayTheMonHoc> findtbl_CTDT_ThayTheMonHocByCondition(
			List<ConditionReport> cons, int pageSize, int page) {
		return _tbl_CTDT_ThayTheMonHocDao.findByCondition(
				Tbl_CTDT_ThayTheMonHoc.class, cons, pageSize, page);
	}

	public int counttbl_CTDT_ThayTheMonHocByCondition(List<ConditionReport> cons) {
		return _tbl_CTDT_ThayTheMonHocDao.countByCondition(
				Tbl_CTDT_ThayTheMonHoc.class, cons);
	}

	public final Tbl_CTDT_ThayTheMonHocDAO get_tbl_CTDT_ThayTheMonHocDao() {
		return _tbl_CTDT_ThayTheMonHocDao;
	}

	public final void set_tbl_CTDT_ThayTheMonHocDao(
			Tbl_CTDT_ThayTheMonHocDAO obj) {
		this._tbl_CTDT_ThayTheMonHocDao = obj;
	}

	public Tbl_CTDT_MonHocDAO get_tbl_CTDT_MonHocDao() {
		return _tbl_CTDT_MonHocDao;
	}

	public void set_tbl_CTDT_MonHocDao(Tbl_CTDT_MonHocDAO _tbl_CTDT_MonHocDao) {
		this._tbl_CTDT_MonHocDao = _tbl_CTDT_MonHocDao;
	}

	public void createListtbl_CTDT_ThayTheMonHocByJSON(String data) {
		try {
			ChumanoLogger.init(Tbl_CTDT_ThayTheMonHocServiceImpl.class.getName());
			ChumanoLogger.log("0BAT DAU INSERT DU LIEU");
			List<Tbl_CTDT_ThayTheMonHoc> tbl_CTDT_TTMonHocs = new ArrayList<Tbl_CTDT_ThayTheMonHoc>();

			JSONArray jsons = (JSONArray) JSONSerializer.toJSON(data);

			for (int i = 0; i < jsons.size(); i++) {
				JSONObject obj = jsons.getJSONObject(i);
				String logStr ="";	
				// MONHOC1 MONHOC2 MONHOCTT1 MONHOCTT2
				// -----------------Lay du lieu--------------------------
				// MONHOC1
				String maMH1 = obj.getString("MONHOC1");

				// MONHOC2
				String maMH2 = obj.getString("MONHOC2");

				// MONHOCTT1
				String maMHTT1 = obj.getString("MONHOCTT1");

				// MONHOCTT2
				String maMHTT2 = obj.getString("MONHOCTT2");
				
				logStr ="[ " + (i+1)+ " - " 
				+ "MAMH1: " + maMH1 + " - " 
				+ "MAMH2: " + maMH2 + " - " 
				+ "MAMHTT1: " + maMHTT1 + " - " 
				+ "MAMHTT2: " + maMHTT2 + " - " 
				+ "]";
				// ======================================================
				// tim mon hoc
				Tbl_CTDT_MonHoc monhoc1 = new Tbl_CTDT_MonHoc();
				List<Long> monhocId1 = findMonHocId(maMH1);
				if (monhocId1 == null || monhocId1.size() == 0) {
					ChumanoLogger.log(logStr + " NOT MAMH1");
					continue;
				}
				monhoc1.setId(monhocId1.get(0));
				
				Tbl_CTDT_MonHoc monhocTT1 = new Tbl_CTDT_MonHoc();
				List<Long> monhocTTId1 = findMonHocId(maMHTT1);
				if (monhocTTId1 == null || monhocTTId1.size() == 0) {
					ChumanoLogger.log(logStr + " NOT MAMHTT1");
					continue;
				}
				monhocTT1.setId(monhocTTId1.get(0));
				
				// ---------
				Tbl_CTDT_MonHoc monhoc2 = null;
				if(maMH2!=null&&!maMH2.equals("")){
					List<Long> monhocId2 = findMonHocId(maMH2);
					if (monhocId2 == null || monhocId2.size() == 0) {
						ChumanoLogger.log(logStr + " NOT MAMH2");
						continue;
					}
					monhoc2 = new Tbl_CTDT_MonHoc();
					monhoc2.setId(monhocId2.get(0));
				}
				Tbl_CTDT_MonHoc monhocTT2 = null;
				if(maMHTT2!=null&&!maMHTT2.equals("")){
					List<Long> monhocTTId2 = findMonHocId(maMHTT2);
					if (monhocTTId2 == null || monhocTTId2.size() == 0) {
						ChumanoLogger.log(logStr + " NOT MAMHTT2");
						continue;
					}
					monhocTT2 = new Tbl_CTDT_MonHoc();
					monhocTT2.setId(monhocTTId2.get(0));
				}
				
				//----------------------------------------
				Tbl_CTDT_ThayTheMonHoc ttmh = new Tbl_CTDT_ThayTheMonHoc();
				List<Long> ids = findTTMonHocId(monhocId1.get(0),monhocTTId1.get(0));
				if (ids != null && ids.size() > 0) {
					ttmh.setId(ids.get(0));
					logStr+=" OLD";
				}
				
				ttmh.setMonHoc1Id(monhoc1);
				ttmh.setMHThayThe1Id(monhocTT1);
				if(monhoc2!=null)ttmh.setMonHoc2Id(monhoc2);
				if(monhocTT2!=null)ttmh.setMHThayThe2Id(monhocTT2);
				
				tbl_CTDT_TTMonHocs.add(ttmh);
				
				ChumanoLogger.log(logStr);

			}// end for
			_tbl_CTDT_ThayTheMonHocDao.saveOrUpdateAll(tbl_CTDT_TTMonHocs);
		} catch (Exception e) {
			ChumanoLogger.log("ERROR: " +e.getMessage());
			System.out.println("------------ERROR CHUMANO-------------");
			e.printStackTrace();
			System.out.println("=================================");
		}
		ChumanoLogger.stop();
	}

	private List<Long> findMonHocId(String maMonHoc) {
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
		{
			ConditionReport con = new ConditionReport();
			con.setCol("ma");
			con.setValue1(maMonHoc);
			con.setOperator("eq");
			cons.add(con);
		}
		List<String> columns = new ArrayList<String>();
		columns.add("id");

		List<Long> monHocId = _tbl_CTDT_MonHocDao.findColumnByCondition(
				Tbl_CTDT_MonHoc.class, cons, columns, 1, 1);
		return monHocId;
	}
	
	private List<Long> findTTMonHocId(Long monhoc1, Long monhocTT1) {
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
		{
			ConditionReport con = new ConditionReport();
			con.setCol("monHoc1Id.id");
			con.setValue1(monhoc1);
			con.setOperator("eq");
			cons.add(con);
		}
		
		{
			ConditionReport con = new ConditionReport();
			con.setCol("MHThayThe1Id.id");
			con.setValue1(monhocTT1);
			con.setOperator("eq");
			cons.add(con);
		}
		List<String> columns = new ArrayList<String>();
		columns.add("id");

		List<Long> Ids = _tbl_CTDT_ThayTheMonHocDao.findColumnByCondition(Tbl_CTDT_ThayTheMonHoc.class, cons, columns, 1, 1);
		return Ids;
	}

}// endclass

